/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */
package org.apache.cordova;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Battery cordova plugin.
 * No real implementation for Desktop.
 * @author Cyril Joui
 */
public class BatteryListener extends CordovaPlugin {

	private CallbackContext batteryCallbackContext = null;

	/**
	 * Executes the request.
	 * 
	 * @param action
	 *            The action to execute.
	 * @param args
	 *            JSONArry of arguments for the plugin.
	 * @param callbackContext
	 *            The callback context used when calling back into JavaScript.
	 * @return True if the action was valid, false if not.
	 */
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
		if (action.equals("start")) {
			if (this.batteryCallbackContext != null) {
				callbackContext.error("Battery listener already running.");
				return true;
			}
			this.batteryCallbackContext = callbackContext;

			// We need to listen to power events to update battery status
			// Not available in Desktop implementation

			// Don't return any result now, since status results will be sent
			// when events come in from broadcast receiver
			PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
			pluginResult.setKeepCallback(true);
			callbackContext.sendPluginResult(pluginResult);
			return true;
		}

		else if (action.equals("stop")) {
			removeBatteryListener();
			this.sendUpdate(new JSONObject(), false); // release status callback
														// in JS side
			this.batteryCallbackContext = null;
			callbackContext.success();
			return true;
		}

		return false;
	}

	/**
	 * Stop the battery receiver and set it to null.
	 */
	private void removeBatteryListener() {
		// No implementation
	}

	/*
	 * Creates a JSONObject with the current battery information
	 * 
	 * @return a JSONObject containing the battery status information
	private JSONObject getBatteryInfo() {
		JSONObject obj = new JSONObject();
		try {
			obj.put("level", 0);
			obj.put("isPlugged", false);
		} catch (JSONException e) {
			// Log.e(LOG_TAG, e.getMessage(), e);
		}
		return obj;
	}
	 */

	/*
	 * Updates the JavaScript side whenever the battery changes
	 * 
	 * @return
	private void updateBatteryInfo() {
		sendUpdate(this.getBatteryInfo(), true);
	}
	 */

	/**
	 * Create a new plugin result and send it back to JavaScript
	 * 
	 * @param connection
	 *            the network info to set as navigator.connection
	 */
	private void sendUpdate(JSONObject info, boolean keepCallback) {
		if (this.batteryCallbackContext != null) {
			PluginResult result = new PluginResult(PluginResult.Status.OK, info);
			result.setKeepCallback(keepCallback);
			this.batteryCallbackContext.sendPluginResult(result);
		}
	}
}
